package com.yf.cassandra.runner;

import com.datastax.driver.core.Session;
import com.datastax.driver.core.exceptions.DriverException;
import com.yf.cassandra.init.InitRunner;
import com.yf.cassandra.utils.ActionTypeEnum;

import java.util.UUID;


/**
 * CapabilityRunnable 性能测试线程
 */
public class CapabilityRunnable implements Runnable {

    private Session session;

    private long average;

    private ActionTypeEnum actionType;

    private String id;

    private Long timestamp;

    public CapabilityRunnable(Session session, long average, ActionTypeEnum actionType, String id, Long timestamp){
        this.session = session;
        this.average = average;
        this.actionType = actionType;
        this.id = id;
        this.timestamp = timestamp;
    }

    public void run() {
        String currentThreadName = Thread.currentThread().getName();
        long failedNum = 0;

        try {
            //执行任务
            for (int i = 1; i <= this.average; i++) {
                try {
                    if(ActionTypeEnum.INSERT == actionType){
                        insert(this.session, this.id, this.timestamp);
                    }else if(ActionTypeEnum.SELECT == actionType){

                    }

                    InitRunner.successCount.incrementAndGet();
                }catch (DriverException e){
                    System.out.println(e.getMessage());
                    failedNum++;
                    if(failedNum>2){
                        // 累计抛出两次错误后就结束线程
                        System.out.println("结束当前线程任务");
                        Thread.currentThread().interrupt();
                        InitRunner.failedCount.incrementAndGet();
                        break;
                    }
                }
            }
        } catch (Exception e) {
            System.out.printf("Thread NO.: %s\n", currentThreadName);
            e.printStackTrace();
        }
    }

    private void insert(Session session, String id, Long timestamp) throws DriverException {
        Object[] values = {id,
                timestamp,
                UUID.randomUUID(),
                "000000000090885",
                "8f688d7392d705c0",
                "TMX08",
                "TMX08",
                "thingsmatrix",
                0,
                80.0f,
                50.0f,
                "0",
                122976528,
                0.0f,
                0,
                715.0f,
                790.0f,
                "BJ51V01191108",
                -1,
                "89014103271540364088",
                "046037944564",
                "162.244.250.180",
                32771,
                29.397633d,
                -98.388016d,
                310,
                410,
                "CAT-M",
                0,
                "[\"SV010\",\"SV010\",\"SV010\",\"SV010\",\"SV010\",\"SV010\",\"SV010\"]",
                -83,
                0.0f,
                -9.4f,
                12.5f};
        session.execute(InitRunner.insertSql, values);
    }
}
